﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using ProjectHT.YKDGVB2012.Core.FeatureAction;
using ProjectHT.YKDGVB2012.Core.MemberShip;
using ProjectHT.YKDGVB2012.Core.Membership.Common;
using ProjectHT.YKDGVB2012.Core.TimeJob;

namespace ProjectHT.YKDGVB2012.Core
{
    public class ConfigSystem : SPFeatureReceiver
    {
        /// <summary>
        /// Implement Event Feature Activated 
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                var site = (SPSite)properties.Feature.Parent;
                var web = site.OpenWeb();
                web.AllowUnsafeUpdates = true;
                CreateList.MainList(web);
                CreateList.CommentList(web);
                MemberShipCommon.CreateList(site);
                //execute Timejob                  
                var webapp = site.WebApplication;
                foreach (SPJobDefinition definition in webapp.JobDefinitions)
                {
                    if (definition.Name == "CheckExpires")
                    {
                        definition.Delete();
                    }
                }

                const string key = "SiteUrl";
                var value = web.Url;

                var docexpire = new CheckExpires(webapp);
                //remove key of timejob
                var isKeyExits = docexpire.Properties.ContainsKey(key);
                if (isKeyExits)
                {
                    docexpire.Properties.Remove(key);
                }

                docexpire.Properties.Add(key, value);

                var schedule = new SPMinuteSchedule {BeginSecond = 0, Interval = 60};
                docexpire.Schedule = schedule;
                docexpire.Update();

                MemberShipCommon.ModifyWebconfig(site.WebApplication, properties.Feature.Definition.Id.ToString());
                web.AllowUnsafeUpdates = false;

            });

        }

        /// <summary>
        ///  Implement Event Feature Deactivating 
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// Implement Event Feature Installed 
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// Implement Event Feature Uninstalling
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            throw new NotImplementedException();
        }
    }
}
